using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HorizonButton : MonoBehaviour
{ 
    public GameObject stoneDoor;
    private Rigidbody2D rb2d;
    public bool right;
    public AudioSource audiosource;
    // Start is called before the first frame update
    public Vector3 ogPosition;

    private void Awake()
    {

        rb2d = stoneDoor.GetComponent<Rigidbody2D>();
    }
    private void Update()
    {
        if (stoneDoor.transform.position.y > -34)
        {
            rb2d.gravityScale = 1;
        }
    }


public void OnTriggerEnter2D(Collider2D collision)
    {
        audiosource.Play();
        rb2d.gravityScale = -1;
        StartCoroutine(ChangeColor(Color.red, right));
    }

    IEnumerator ChangeColor(Color color, bool up)
    {
        Vector3 deltaV;
        if (up)
        {
            deltaV = new Vector3(0.2f, 0, 0);
        }
        else
        {
            deltaV = new Vector3(-0.2f, 0, 0);
        }
        SpriteRenderer sr = GetComponent<SpriteRenderer>();
        float time = 0;
        Color deltaColor = color - sr.color;
        while (time < 0.5f)
        {
            time += Time.deltaTime;
            transform.position += deltaV * Time.deltaTime;
            sr.color += deltaColor * (Time.deltaTime / 0.5f);
            yield return null;
        }

        sr.color = color;
    }
    public void OnTriggerExit2D(Collider2D collision)
    {
        StartCoroutine(ChangeColor(Color.white, !right));
    }
}

